<?php

if(PHP_SAPI != 'cli')
{
    die("Please run script with cli mode!");
}

define("DS" , DIRECTORY_SEPARATOR);
define("ROOT" , dirname(__FILE__));

define('LIB' , ROOT . DS . 'library');
define('MODULE', ROOT . DS . 'modules');
define('RESULT', ROOT . DS . 'result');

if(!in_array('create', $argv))
{
    die('create need');
}

if(!isset($argv[2]) || !preg_match('/[a-zA-Z]+/i', $argv[2]))
{
    die('project names is required!');
}

define('PROJECT_NAME', $argv[2]);
define('PROJECT_PATH', RESULT . DS . PROJECT_NAME);

if(!is_writable(PROJECT_PATH))
{
    @mkdir(PROJECT_PATH, 755);
    recurse_copy(LIB . DS . 'framework' , PROJECT_PATH , 1);
}


$controller_variable = '{$controller_name}';
$table_list = array('article', 'user');
$configs = require 'config.php';
$pre_fix = $configs['db_prefix'];


require LIB . DS . 'Controller.php';
require LIB . DS . 'Model.php';
require LIB . DS . 'View.php';
require LIB . DS . 'Database.php';

$configs['database'] = PROJECT_NAME;
$database = new Database($configs);
$table_list = $database->getTables();

print_r($table_list);

if($table_list)
{
    foreach($table_list as $table)
    {
        $columns = $database->getColumn($table);

        $table = str_replace($pre_fix, '', $table);
        $model_name = ucfirst($table);

        $con = new Controller($model_name, $columns);
        $mod = new Model($model_name, $pre_fix, $columns);
        $view = new View($model_name);

    }
}



function recurse_copy($src,$des)
{
    $dir = opendir($src);
    @mkdir($des);
    while(false !== ( $file = readdir($dir)) )
    {
        if (( $file != '.' ) && ( $file != '..' ))
        {
            if ( is_dir($src . '/' . $file) )
            {
                recurse_copy($src . '/' . $file,$des . '/' . $file);
            }  else  {
                copy($src . '/' . $file,$des . '/' . $file);
            }
        }
    }
    closedir($dir);
}



